var tools = "";

//标记用户是否点击了查询按钮，用于控制评价。在点击了查询后将其赋值为1
var mark = 0;
$(function(){
	var pagePath =path;
	//列表加载
		$("#historyTable").initTable({
	        url:pagePath+"/pig/cleanPipeLineHistory/getDataList.json",
	        columnData:[
	            {title:tableOpt.getCheckBox(null),name:"ID",width:"",align:"center",format:function(json){return tableOpt.getCheckBox(json.ID)}},
	            {title:"任务名称",name:"TASKNAME",width:"10%",align:"center"},
	            {title:"管道名称",name:"LINENAME",width:"15%",align:"center"},
	            {title:"发球点",name:"FROMPIG",width:"10%",align:"center"},
	            {title:"收球点",name:"TOPIG",width:"10%",align:"center"},
	            {title:"发球时间",name:"BEGTIME",width:"15%",align:"center"},
	            {title:"创建人",name:"CREATEUSER",width:"10%",align:"center"},
	            {title:"状态",name:"STAUS",width:"10%",align:"center",format:function(json){ return tableOpt.getState(json.STAUS)}},
	            {title:"详情",name:"ID",width:"5%",align:"center",format:function(json){return tableOpt.getDetail(json.ID)}},
				{title:"附件",name:"ID",width:"5%",align:"center",format:function(json){return tableOpt.getDownload(json.ID)}}
	        ],
	        selectDataNow:true
	    });
	 
});

//查询
function searchFun(){
	var lineId =  $pipeline.val();
	var fromValId =$begin.val();
	var toValId =$end.val();
	var pigType = $("#toolDiv").children("a").attr("id") == "qglx" ? "" : $("#toolDiv").children("a").attr("id")
	var begTime = $("#begTime").val();
	var endTime = $("#endTime").val();
	var data = {"taskName":$.trim($("#taskName").val()),"lineId":lineId,"fromValId":fromValId,"toValId":toValId,"begTime":begTime,"endTime":endTime,"pigType":pigType};
	$('#historyTable').selectDataAfterSetParm(data);
//	var compareName = "漏磁检测,超声波检测,几何检测";
//	var compareName = "生产清管,智能检测前清管,其它";
//	if(compareName.indexOf($("#toolDiv").children("a").html()) >= 0){
//		$("#evluationId").css({display:'block'});
//	}else{
//		$("#evluationId").css({display:'none'});
//	}
	mark = 1;
}

var tableOpt = {
		getCheckBox:function(id){
			var name = null == id ? 'checkAll' : 'checkSingle';
			var html = "<li style='list-style-type:none;' class='check-box selected '><span class='table-ck'><span id='"+id+"' name='"+name+"' class='check-box selected' onclick=''><i></i></span></span></li>";
			return html;
		},
		
		getState:function(data){
			return "已完成";
		},
		
		getDetail:function(data){
			var html = "<li style='list-style-type:none;' class='w-q6'><a href='javascript:void(0);' class='jl_00' onclick='getDetail(\""+data+"\");'></a></li>";
			return html;
		},
		getDownload:function(data){
			var html = "<li style='list-style-type:none;' class='w-q6'><a href='javascript:void(0);' class='download_00' onclick='downloadTaskFile(\""+data+"\");'></a></li>";
			return html;
		}
}

function getDetail(obj){
	var pagePath = path;
	var taskId = obj;
	if(null == taskId || "" == taskId || undefined == taskId ){
		$(this).MSG({
			type: 'error',
			content: '操作失败，请刷新页面后重试！',
		});
		return ;
	}else{
		window.location.href = pagePath+"/pig/cleanPipeLineHistory/getHistoryDetail.html?taskId="+taskId;
	}
}


/* 日期格式化 */
Date.prototype.Format = function(fmt) {
    var o = {
        "M+" : this.getMonth() + 1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
    for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1,
                    (RegExp.$1.length == 1) ? (o[k])
                            : (("00" + o[k])
                            .substr(("" + o[k]).length)));
    return fmt;
};

//获取管段
function getInspSegment(lineId){
	$("#inspSegment").html("");
	var pagePath = path;
	var url = pagePath + "/pig/cleanPipeLineManager/getInspSegment.json";
	$().invoke(url,{"lineId":lineId},[function(m,r){
		var inspSegment = r;
		var html='';
		$.each(inspSegment,function(i,evalue){
			html += "<li onclick='changeMark();'><a href='javascript:void(0);' id='"+ evalue.id+"'>"+ evalue.name+"</a></li>";
		});
		$("#segmentDiv").html("<a href='javascript:void(0);' id='jcgdmc'>管段名称</a><span></span>");
		$("#inspSegment").html(html);
	}]);
}


function goEvluation(){
	var pagePath =path;
	var childBox = $("span[name=checkSingle]");
	var count = 0;
	var idsArr = new Array();
	
	var lineId= $pipeline.val();
	var fromValId =$begin.val();
	var toValId =$end.val();
	var cleanTypeName = $("#toolDiv").children().attr("id");
//	var compareName = "生产清管,智能检测前清管,其它";
	for(var i= 0 ; i < childBox.length;i ++){
		if(childBox.eq(i).children().hasClass("active")){
			idsArr.push(childBox[i].id);
			count++;
		}
	}
	if(null == lineId || "" == lineId || undefined == lineId){
		$(this).MSG({content:'请选择管道'});
	}
	if(null== fromValId || "" == fromValId || undefined == fromValId){
		$(this).MSG({content:'请选择起点位置'});
	}
	if(null== toValId || "" == toValId || undefined == toValId){
		$(this).MSG({content:'请选择终点位置'});
		return ;
	}
	if("清管类型" == cleanTypeName){
		$(this).MSG({content:'请选择清管类型'});
		return ;
	}else if(mark == 0){
		$(this).MSG({content:'请先点击查询获取数据列表'});
	}else if(idsArr.length < 1){
		$(this).MSG({content:'请先勾选需要评价的任务'});
		return ;
	}else{
		var idStr = idsArr.join();
		window.location.href = pagePath + "/pig/cleanPipeLineHistory/goEvluation.html?ids="+idStr;
	}
}


function changeMark(){
	if(mark == 1){
		mark = 0;
	}
}

/**
 * 下载任务附件
 */
function downloadTaskFile(taskId){
	var pagePath = path;
	var url = pagePath + "/pig/cleanPipeLineManager/downloadTaskFile.json?taskId="+taskId;
	backgroundExport(url);
}